package demo2;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-06-26
 * Time: 17:07
 */
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> Pq = new PriorityQueue<Integer>(new Comparator<Integer>(){
            public int compare(Integer o1,Integer o2){
                return o2-o1;
            }
        });
        int tmp =0;
        for(int e:stones){
            Pq.offer(e);
        }
        while(Pq.size()>1){
            int x = Pq.poll();
            int y = Pq.poll();
            if(x>y){
                tmp = x-y;
                Pq.offer(tmp);
            }else if(x<y){
                tmp =y-x;
                Pq.offer(tmp);
            }
        }
        if(Pq.isEmpty()){
            return 0;
        }
        return Pq.peek();
    };
}
public class TestDmo {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] stones = {2,7,4,1,8,1};
        System.out.println(solution.lastStoneWeight(stones));
    }
}
